package com.dstz.demo.rest.controller;


import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.demo.core.manager.OrderbusService;
import com.dstz.demo.core.model.Orderbus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@RestController
@RequestMapping("/sys/Bus")
public class OrderBusController {

    @Resource
    private OrderbusService OrderbusService;
    @PostMapping("/orderBus")
    public ResultMsg<String> insert(String username,String phone,String Stationup,String Stationdown,String peoplenumber,String choosetime) throws ParseException {
        Orderbus Orderbus1=new Orderbus();
        Orderbus1.setOrdertime(new Date());
        Orderbus1.setUsername(username);
        Orderbus1.setPhone(phone);
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(choosetime);
        Orderbus1.setChoosetime(date);
        Orderbus1.setStationup(Stationup);
        Orderbus1.setStationdown(Stationdown);
        Orderbus1.setPeoplenumber(Integer.parseInt(peoplenumber));
        Orderbus1.setOrdernum(this.doOrderNum());//生成订单号方法
        Orderbus1.setStatus(1);//1下单状态  //2已读
        String data=OrderbusService.insert(Orderbus1);
        return  new ResultMsg<>(data);
    }

    @GetMapping("/getOderBus/{id}")
    public ResultMsg<Orderbus> getOderBus(@PathVariable("id")Integer id){

        Orderbus data=OrderbusService.getByIdOrderbus(id);

        return new ResultMsg<>(data);
    }



    /**
     * 生成订单号
     *
     * @return
     */
    protected String doOrderNum() {
        Random random = new Random();
        SimpleDateFormat allTime = new SimpleDateFormat("YYYYMMddHHmmSSS");
        String subjectno = allTime.format(new Date())+random.nextInt(10);
        return subjectno+random.nextInt(10);
    }




}
